package cn.stylefeng.guns.sys.modular.tao.controller;

import cn.stylefeng.guns.sys.modular.api.sdk.Utils;
import cn.stylefeng.guns.sys.modular.tao.service.WxUserService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/wxUser")
public class WxUserController {

    @Autowired
    private WxUserService wxUserService;

    @Autowired
    private Gson gson;

    /**
     * 注册
     */
    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String regiester(@RequestParam("loginPassword") String loginPassword,@RequestParam("registerNumber") String registerNumber,@RequestParam("promoterId")Long promoterId) {
        if (Utils.checkStringIsNull(loginPassword,registerNumber)) {
            ResponseData dataMsg = new ResponseData();
            dataMsg.setMessage("参数不能为空");
            dataMsg.setCode(500);
            return gson.toJson(dataMsg);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("loginPassword", loginPassword);
        map.put("registerNumber", registerNumber);
        map.put("promoterId", promoterId);
        ResponseData data = wxUserService.register(map);

        return gson.toJson(data);
    }

    /**
     * 账号登录
     */
    @RequestMapping(value = "/loginBypassword", method = RequestMethod.GET)
    public String loginBypassword(@RequestParam("loginPassword") String loginPassword, @RequestParam("registerNumber") String registerNumber) {
        if (Utils.checkStringIsNull(loginPassword, registerNumber)) {
            ResponseData dataMsg = new ResponseData();
            dataMsg.setMessage("参数不能为空");
            dataMsg.setCode(500);
            return gson.toJson(dataMsg);
        }
        Map<String, String> map = new HashMap<>();
        map.put("loginPassword", loginPassword);
        map.put("registerNumber", registerNumber);
        ResponseData data = wxUserService.loginBypassword(map);

        return gson.toJson(data);
    }

    /**
     * 微信登录
     */
    @RequestMapping(value = "/loginByWx", method = RequestMethod.GET)
    public String loginByWx(@RequestParam("code") String code, @RequestParam("encryptedData") String encryptedData, @RequestParam("iv") String iv,@RequestParam("promoterId")Long promoterId) {
        if (Utils.checkStringIsNull(code, encryptedData, iv)) {
            ResponseData dataMsg = new ResponseData();
            dataMsg.setMessage("参数不能为空");
            dataMsg.setCode(500);
            return gson.toJson(dataMsg);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("encryptedData", encryptedData);
        map.put("code", code);
        map.put("iv", iv);
        map.put("promoterId", promoterId);
        ResponseData data = wxUserService.loginByWx(map);

        return gson.toJson(data);
    }

    /**
     * 设置/修改登录密码
     */
    @RequestMapping(value = "/updateLoginPasswoed", method = RequestMethod.GET)
    public String updateLoginPasswoed(@RequestParam("loginPassWord") String loginPassWord, @RequestParam("oldPassWord") String oldPassWord, @RequestParam("id") Long id) {
        if (Utils.checkStringIsNull(loginPassWord,id.toString())) {
            ResponseData dataMsg = new ResponseData();
            dataMsg.setMessage("参数不能为空");
            dataMsg.setCode(500);
            return gson.toJson(dataMsg);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("loginPassWord", loginPassWord);
        map.put("oldPassWord", oldPassWord);
        map.put("id", id);
        ResponseData data = wxUserService.updateLoginPasswoed(map);

        return gson.toJson(data);
    }

    /**
     * 实名认证
     */
    @RequestMapping(value = "/updateAuthentication", method = RequestMethod.GET)
    public String updateAuthentication(@RequestParam("idNumber") String idNumber,@RequestParam("realeName") String realeName, @RequestParam("id") Long id) {
        if (Utils.checkStringIsNull(idNumber, id.toString(),realeName)) {
            ResponseData dataMsg = new ResponseData();
            dataMsg.setMessage("参数不能为空");
            dataMsg.setCode(500);
            return gson.toJson(dataMsg);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("idNumber", idNumber);
        map.put("id", id);
        map.put("realeName", realeName);
        ResponseData data = wxUserService.updateAuthentication(map);

        return gson.toJson(data);
    }

    /**
     *会员升级
     */
    @RequestMapping(value = "/updateMember",method = RequestMethod.GET)
    public String updateMember(@RequestParam("memberGrade") String memberGrade,@RequestParam("id") Long id){
        if (Utils.checkStringIsNull(memberGrade, id.toString())) {
            ResponseData dataMsg = new ResponseData();
            dataMsg.setMessage("参数不能为空");
            dataMsg.setCode(500);
            return gson.toJson(dataMsg);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("memberGrade", memberGrade);
        map.put("id", id);
        ResponseData data = wxUserService.updateMember(map);

        return gson.toJson(data);
    }

    /**
     * 会员过期
     */
    @RequestMapping(value = "/deleteMember",method = RequestMethod.GET)
    public String updateMember(@RequestParam("id") Long id){
        if (Utils.checkStringIsNull(id.toString())) {
            ResponseData dataMsg = new ResponseData();
            dataMsg.setMessage("参数不能为空");
            dataMsg.setCode(500);
            return gson.toJson(dataMsg);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        ResponseData data = wxUserService.deleteMember(map);

        return gson.toJson(data);
    }

    /**
     * 生成会员推广码
     */
    @RequestMapping(value = "/getWxUserQRCode",method = RequestMethod.GET)
    public String getWxUserQRCode(@RequestParam("id") Long id){
        if (Utils.checkStringIsNull(id.toString())) {
            ResponseData dataMsg = new ResponseData();
            dataMsg.setMessage("参数不能为空");
            dataMsg.setCode(500);
            return gson.toJson(dataMsg);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        ResponseData data = wxUserService.getWxUserQRCode(map);
        return gson.toJson(data);
    }

    /**
     *查询用户信息
     */
    @RequestMapping("/findUserInfo")
    public ResponseData findUserInfo(Long id){
        if (Utils.checkStringIsNull(id.toString())) {
            ResponseData dataMsg = new ResponseData();
            dataMsg.setMessage("参数不能为空");
            dataMsg.setCode(500);
            return dataMsg;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        ResponseData data = wxUserService.findUserInfo(map);
        return data;
    }

}
